
package com.heimali.happyreader.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heimali.happyreader.HappyReaderApplication;
import com.heimali.happyreader.R;
import com.heimali.happyreader.core.bean.Article;
import com.heimali.happyreader.core.intf.ITaskListenerSimple;
import com.heimali.happyreader.core.manager.GlobalManager;
import com.heimali.happyreader.ui.fragment.helper.CommonListFragmentHelper;
import com.heimali.happyreader.utils.AndroidUtils;
import com.heimali.happyreader.utils.FileUtils;
import com.heimali.happyreader.utils.NetCheckUtils;

public class DetailActivity extends Activity implements OnClickListener {
    public static final String TAG = "DetailActivity";
    public static final String INTENT_DETAIL_ID_KEY = "article_id";

    private static final String mimeType = "text/html";
    private static final String charset = "utf-8";

    private LinearLayout mProgressBarLayout;
    private TextView mTitleText;
    private WebView mWebView;
    private int mArticleID;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        NetCheckUtils.registerDataTransReceiver(new NetChangeReceiver());
        initView();
        loadActicle();
    }

    private void initView() {
        ((Button) findViewById(R.id.left_btn)).setOnClickListener(this);
        mProgressBarLayout = (LinearLayout)findViewById(R.id.progressbar_layout);
        mTitleText = (TextView) findViewById(R.id.top_title);
        mWebView = (WebView) findViewById(R.id.article);
    }

    private void loadActicle() {
        mArticleID = getIntent().getIntExtra(INTENT_DETAIL_ID_KEY, 0);
        if (mArticleID <= 0) {
            return;
        }
        GlobalManager.getInstance().articleManager.asyncLoadDetail(new LoadActicleListener(), mArticleID);
    }

    private void showArticle(Article article) {
        if (article == null) {
            return;
        }

        mTitleText.setText(getDomain(article.sourceUrl));
        String content = FileUtils.readAssetsFile(DetailActivity.this, "article.html");
        content = content.replace("{$title}", article.title).replace("{$content}", article.content);
        mWebView.loadDataWithBaseURL(null, content, mimeType, charset, null);
    }

    private String getDomain(String sourceUrl) {
        if (TextUtils.isEmpty(sourceUrl)) {
            return "";
        }

        int idx = sourceUrl.indexOf("//");
        if (idx <= 0) {
            return "";
        }
        String str = sourceUrl.substring(idx + 2, sourceUrl.length());

        idx = str.indexOf("/");
        if (idx <= 0) {
            return str;
        }
        return str.substring(0, idx);
    }

    class LoadActicleListener implements ITaskListenerSimple {
        @Override
        public Object taskFinished(Object param) {
            showArticle((Article) param);
            mProgressBarLayout.setVisibility(View.GONE);
            mWebView.setVisibility(View.VISIBLE);
            return null;
        }

        @Override
        public Object taskFailed(Object param) {
            mProgressBarLayout.setVisibility(View.GONE);
            CommonListFragmentHelper.showFailedError(param);
            return null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                finish();
                break;
        }
    }
    
    class NetChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isNetworkAvailable = NetCheckUtils.isNetworkAvailable();
            if (HappyReaderApplication.isNetworkAvailable == isNetworkAvailable) {
                return ;
            }
            HappyReaderApplication.isNetworkAvailable = isNetworkAvailable;
            
            if (isNetworkAvailable) {
                AndroidUtils.toast(R.string.network_available);
                loadActicle();
            }else {
                AndroidUtils.toast(R.string.network_not_available);
            }
        }
    }
}
